﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using The_Square_Saga.Objects.Interface;

namespace The_Square_Saga.Objects
{
    public enum DragonState
    {
        stay,
        taking_off,
        flying,
        landing,

    }
    class Dragon : AnimatedGameEntity, IGameEntity
    {
        /// <summary>
        /// The current state
        /// </summary>
        private DragonState currentState;

        /// <summary>
        /// A direction
        /// </summary>
        private Vector2 direction;

        /// <summary>
        /// The velocity for move
        /// </summary>
        private float velocity;

        public Dragon(Texture2D image, Vector2 position, Vector2 frameSize, float velocity)
            : base(image, position)
        {
            this.velocity = velocity;
        }

        /// <summary>
        /// Deal with the input, set direction and player state
        /// </summary>
        private void handleInput()
        {
            DragonState pState = currentState;
            direction = Vector2.Zero;

            if (Game1.keyboardState.IsKeyDown(Keys.Right))
            {
                this.deFlipHorizontally();
                direction = new Vector2(1, 0);

                if (pState == DragonState.stay)
                {
                    pState = DragonState.taking_off;
                }
            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Left))
            {
                this.flipHorizontally();
                direction = new Vector2(-1, 0);

                if (pState == DragonState.stay)
                {
                    pState = DragonState.taking_off;
                }
            }
            else if (Game1.keyboardState.IsKeyUp(Keys.Right) && Game1.previusKeyboardState.IsKeyDown(Keys.Right))
            {
                if (pState == DragonState.flying)
                {
                    pState = DragonState.landing;
                }
            }
            else
            {
                if (currentState == DragonState.landing)
                {
                    if (this.isAnimationFinished())
                    {
                        pState = DragonState.stay;
                    }
                }
                else
                {
                    pState = DragonState.stay;
                }
            }

            if (pState != currentState)
            {
                currentState = pState;
            }
        }

        public override void update(GameTime gameTime)
        {
            handleInput();

            if (currentState == DragonState.taking_off)
            {
               // this.playAnimation("takingOff", gameTime);

                if (this.isAnimationFinished())
                {
                    currentState = DragonState.flying;
                }
            }
            else if (currentState == DragonState.flying)
            {
                this.position += velocity * direction;
               // this.playAnimation("flying", gameTime);
            }
            else if (currentState == DragonState.landing)
            {
                //  this.playAnimation("landing", gameTime);
            }
            else
            {
                // this.playAnimation("stay", gameTime);
            }
        }
    }
}
